package service

import (
	"go_web_todo/dao"
	"go_web_todo/models"
	"go_web_todo/pkg/e"
	"go_web_todo/serializer"
	"time"

	logging "github.com/sirupsen/logrus"
)

//展示任务详情的服务
type ShowTaskService struct {
}

//删除任务的服务
type DeleteTaskService struct {
}

//更新任务的服务
type UpdateTaskService struct {
	ID      uint   `form:"id" json:"id"`
	Title   string `form:"title" json:"title" binding:"required,min=2,max=100"`
	Content string `form:"content" json:"content" binding:"max=1000"`
	Status  int    `form:"status" json:"status"` //0 待办   1已完成
}

//创建任务的服务
type CreateTaskService struct {
	Title   string `form:"title" json:"title" binding:"required,min=2,max=100"`
	Content string `form:"content" json:"content" binding:"max=1000"`
	Status  int    `form:"status" json:"status"` //0 待办   1已完成
}

//搜索任务的服务
type SearchTaskService struct {
	Info string `form:"info" json:"info"`
}

type TaskListService struct {
	Limit int `form:"limit" json:"limit"`
	Start int `form:"start" json:"start"`
}

func (service *CreateTaskService) CreateTask(id uint) serializer.Response {
	var user models.User
	dao.FindUserByID(id, &user)
	task := models.Task{
		User:      user,
		Title:     service.Title,
		Content:   service.Content,
		Status:    0,
		StartTime: time.Now().Unix(),
	}
	code := e.SUCCESS
	if err := dao.CreateTask(&task); err != nil {
		logging.Info(err)
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Data:   serializer.BuildTask(task),
		Msg:    e.GetMsg(code),
	}
}
func (service *TaskListService) GetTaskList(uid uint) serializer.Response {
	var tasks []models.Task
	var total int64 = 0
	if service.Limit == 0 { //设置一页展示多少条数据
		service.Limit = 15
	}
	dao.GetTasksByUid(&tasks, service.Limit, service.Start, uid, &total)
	return serializer.BuildListResponse(serializer.BuildTasks(tasks), uint(total))
}
func (service *ShowTaskService) ShowTaskDetails(id string) serializer.Response {
	var task models.Task
	code := e.SUCCESS
	if err := dao.GetTaskDetailById(&task, id); err != nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	task.AddView() //增加当前任务的点击数
	return serializer.Response{
		Status: code,
		Data:   serializer.BuildTask(task),
		Msg:    e.GetMsg(code),
	}
}
func (service *DeleteTaskService) DeleteTaskById(id string) serializer.Response {
	code := e.SUCCESS
	if err := dao.DeleteTaskById(id); err != nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Msg:    e.GetMsg(code),
	}
}
func (service *UpdateTaskService) UpdateTask(id string) serializer.Response {
	var task models.Task
	dao.GetTaskDetailById(&task, id)
	task.Content = service.Content
	task.Status = service.Status
	task.Title = service.Title
	code := e.SUCCESS
	if err := dao.UpdateTask(&task); err != nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Msg:    e.GetMsg(code),
		Data:   "修改成功！",
	}
}
func (service *SearchTaskService) SearchTask(uid uint) serializer.Response {
	var tasks []models.Task
	code := e.SUCCESS
	dao.GetTaskByUid(uid, &tasks) //预加载User
	if err := dao.SearchTaskByTitleOrContent(service.Info, &tasks); err != nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Msg:    e.GetMsg(code),
		Data:   serializer.BuildTasks(tasks),
	}
}
